yjx.controller('editRelationController', ['$rootScope', '$scope', '$fetch', 'modalService', '$stateParams', '$timeout', function ($rootScope, $scope, $fetch, modalService, $stateParams, $timeout) {
    var clientListApi = '/customer/relation/selected';
    //$scope.$parent.customerId = $stateParams.customerId;
    //$scope.name = $stateParams.name;
    $scope.title = "dd";
    /*获取客户列表*/
    function getClientList() {
        $fetch.fetch(
            clientListApi,
            {
                params: {
                    // userId: $rootScope.userId,
                    customerId: $scope.$parent.customerId
                },
                // type: 'POST',
                cache: false,
            }
        ).then(function (data) {
            $timeout(function () {
                if (data ) {
                    $scope.share.clientList = data;
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    if ($scope.$parent.customerId) {
        // $scope.share.clientList = [{"id":17,"name":"乎乎","sex":"男","createTime":null,"telephone":"13890403456","income":"20k - 25k","job":"医疗","address":null,"remark":null,"bindUserId":0,"tagId":null,"tagValue":[]},{"id":18,"name":"我","sex":"男","createTime":null,"telephone":"13589304758","income":"20k - 25k","job":"医疗","address":null,"remark":"得治","bindUserId":0,"tagId":null,"tagValue":[]}];
        getClientList();
    }
 
    /**选择关系 */
    $scope.relationSelect = function (value, id) {
        $timeout(function () {
            $scope.relationSelected = $scope.relationships[id]
        })
    }
    /**选择客户 */
    $scope.clientSelect = function (item) {
        for (var i = 0; i < $scope.share.clientList.length; i++) {
            if ($scope.share.clientList[i].checkit) {
                $scope.share.clientList[i].checkit = false;
                break;
            }
        }
        item.checkit = true;
        $scope.selectedClient = item;
    }
    /*请输入与目标客户的关系*/
    $scope.addRelationAct = function () {
        $fetch.fetch(
            '/customer/relation/add',
            {
                params: {
                    userId: $rootScope.userId,
                    relationId: $scope.relationSelected.id,
                    id: $scope.selectedClient.id,
                    parentId: $scope.$parent.customerId
                },
                type: 'POST'
            }
        ).then(function (data) {
            $timeout(function () {
                inputSuc(data);
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }

    /*添加成功*/
    function inputSuc(data) {
        if ($scope.share.clientList) {
            for (var i in $scope.share.clientList) {
                if ($scope.selectedClient.id == $scope.share.clientList[i].id) {
                    if ($scope.share.relationList) {
                        $scope.share.clientList[i].customerRelationId = data;
                        $scope.share.clientList[i].relation = $scope.relationSelect.value;

                        $scope.share.relationList.push($scope.share.clientList[i]);
                        $scope.share.clientList.splice(i, 1);
                        $timeout(function(){ $scope.$parent.resetHeight();})/**解决页面高度没更新问题 */
                        history.back();
                        break;
                    }
                    //else{
                    //    $scope.share.clientList[i].relationId =data;
                    //    $scope.share.clientList[i].relationName =$scope.selectedClient.relationName;
                    //    $scope.share.relationList.push($scope.share.clientList[i])
                    //    $scope.share.relationList = new Array($scope.share.clientList[i]);
                    //    console.log( $scope.share.clientList[i].tag);
                    //    $scope.share.clientList.splice(i,1);
                    //    //$timeout(function(){
                    //    //    history.back();
                    //    //})
                    //    /*初始化选择的数据*/
                    //    initInputData();
                    //    break;
                    //}
                }
            }
        }
    }
    /**获取关系字典数据 */
    function getDictionary(){
        var code = ['relation'];
        $fetch.fetch(
            '/common/base/dict',
            {
                params:{
                    code:code,
                }
            }
        ).then(function(data){
            $timeout(function(){
                $scope.relationships = data.relation;
                $scope.relationData = [$scope.relationships];
                $scope.relationIndex = [Math.floor($scope.relationships.length / 2)];
            })
        }).catch(function(e){
            modalService.mainMsgError(e.msg);
        })
    }
    getDictionary();
}])